﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace Fitment
{
    public class DS
    {
        public static SqlConnection conn;
        string strConnection = ConfigurationManager.ConnectionStrings["connection"].ToString();//读取配置文件连接信息
        public DS()
        {
            conn = new SqlConnection(strConnection);   ///创建数据库连接
        }
        #region 返回执行SQL语句的操作布尔值

        /// <summary>
        /// 执行SQL语句，成功就返回true
        /// </summary>
        /// <param name="sqlStr"></param>
        /// <returns></returns>
        public string ExcuteLogin(string sqlStr)
        {
            SqlCommand cmd = new SqlCommand(sqlStr, conn);
            try
            {
                conn.Open();
                string str;
                //string str = cmd.ExecuteScalar().ToString(); //执行查询，并返回查询结果的第一行第一列
                object obj = cmd.ExecuteScalar();
                if (obj == null)
                    str = "no&found";
                else
                    str = obj.ToString();
                return str;
            }
            catch (SqlException ex)
            {
                string error = ex.ToString();
                return error;
            }
            finally
            {
                conn.Close();
                cmd.Dispose();
            }
        }
        #endregion
        #region 返回操作sql语句的布尔值

        /// <summary>
        /// 执行sql语句,用于插、删、改语句
        /// </summary>
        /// <param name="strConnection"></param>
        /// <param name="sqlSelect"></param>
        public bool ExcuteSql(string sql)
        {
            int Rows = 0;

            try
            {
                conn.Open();
                SqlCommand command = new SqlCommand(sql, conn);
                Rows = command.ExecuteNonQuery();

                if (Rows == 1||Rows==2||Rows==3)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (SqlException ex)
            {
                string error = ex.ToString();
                return false;
            }
            finally
            {
                conn.Close();
            }
        }
        #endregion

        #region 执行Sql语句，返回DataTable

        /// <summary>
        /// 生成数据表
        /// </summary>
        /// <param name="Sql">查询的SQL语句</param>
        /// <param name="TableName">填充的表名</param>
        /// <returns></returns>
        public DataTable GetDataSource(string Sql, string TableName)
        {

            DataSet ds = new DataSet();

            SqlDataAdapter da = new SqlDataAdapter(Sql, conn);
            try
            {
                da.Fill(ds, TableName);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
            finally
            {
                conn.Close();
                da.Dispose();
            }
            DataTable result = ds.Tables[TableName];
            return result;
        }
        #endregion
    }
}
